<?php
require DT_CORE.'/buyhelp/common.inc.php';
defined('IN_IXDCW') or exit('Access Denied');

$navname     = 'picture';
$MOD['name'] = '买车助手_图片';


for($i=0; $i<=2; $i++) {
	$tablePic   = $db->getPre() . 'auto_picture_' . $i;
	$tableArr[] = " select * from $tablePic ";
}



function unionSql($tableArr,$where,$limit) {
	foreach($tableArr as $key => $val){
		$tableArr[$key] = '('.$val.$where.')';
	}
	$sql = implode(' union ',$tableArr).$limit;
	return $sql;
}


if($pictureid) {
	$carId     = isset($carid) ? intval($carid) : 0;
	$pictureId = isset($pictureid) ? intval($pictureid) : 0;
	$modelId   = isset($modelId) ? intval($modelId) : 0;

	if (!$carId) {
		$modelInfo = AutoModel::get($modelId);
		$carId = $modelInfo['arcid'];
	}

	//打开页面后，首张图片信息
	$pictureInfo = AutoPicture::getPicturesInfo($carid, $pictureid);

	if (!$alid) {
		$alid = $pictureInfo['alid'];
	}
	//品牌
	$brandId   = AutoCar::getBrandId($carid);
	$brandName = Auto::getName($brandId);

	//生产商
	$manufactorId   = AutoCar::getManufactorId($carid);
	$manufactorName = Auto::getName($manufactorId);

	//车系
	$carName = Auto::getName($carid);

	//车款名称
	$modelArr = Auto::getManyModelName($carid);
	foreach ($modelArr as $val) {
		$modelName[$val['amid']] = $val['name'];
	}


	//接收选中车款的传值
	if (isset($a) && $a != 0) $amid = $a;

	//车款id
	if (isset($modelid) && $modelid != 0) $amid = $modelid;

	$pictureCats = AutoPicture::getPicturesShow($carid,$amid);
	//输出
	$pictureShowInfos     = AutoPicture::getPicturesAPI($carid, $amid, $pictureId, $alid);

	include template('pictureShow', 'auto');

}else{

	$autoId = AutoCategoryModule::getCarbylevel($cartypeid);

	$tableC = Table::name(Table::NAME_AUTO_RANK_CAR);
	$tableCat = Table::name(Table::NAME_AUTO_CATEGORY);

	/*最新车系图片*/
	$sql = "select c.arid as arcid, cat.name as title, c.car_thumb as imgurl from $tableC c, $tableCat cat where c.arid=cat.arid and c.car_thumb!='' and c.arid in($autoId) order by c.arid desc limit 20";
	$latestPic = $db->getAll($sql);

	/*过滤图片库 无图片车系*/
	foreach($latestPic as $key => $val){
		$cid = $val['arcid'];
		$table = Table::autoPicture($cid);
		if(!$db->getVar("select apid from $table where arcid=$cid")) unset($latestPic[$key]);
	}
	$latestPic = array_slice($latestPic,0,10);

	/*图片类别 类别图片*/
	$RANK = Pcache::read('autoCategory-'.AutoCategory::TYPE_PICTURE.'.php');
	foreach($RANK as $key => $val) {
		if($val['parentid'] == 0) {
			$pictureType[$key]['arid'] = $val['arid'];
			$pictureType[$key]['name'] = $val['name'];
			$pictureType[$key]['arrchildid'] = $val['arrchildid'];
			$pictureType[$key]['arr'] = $db->getAll(unionSql($tableArr," where alid in (".$val['arrchildid'].") and arcid in($autoId) group by arcid "," limit 5"));
		}
	}

	include template('pictureList','buyhelp');
}


